﻿
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;

using Might.Core.DOM;
using Might.Core.Services;
using Might.Infrastructure.DOM;
using Might.Infrastructure.Services;
using Might.Modelica.Annotations;
using Might.Modelica.Parsers;

namespace Might.Core {

    /// <summary>
    /// Module of application core. Offers core services like acces to Abstract syntax tree (AST).
    /// </summary>
    public class CoreModule : IModule {
            
        private IRegionManager regionManager;
        private readonly IUnityContainer container;
        private readonly IEventAggregator eventAggregator;
        private DomRoot dom;
        private IconProvider iconProvider;

        private IAnotTypeRegister typeRegister;
        private AuthenticationService authService;  
        private SVNFileSystemService svnService;
        private LocalFileSystemService localFileSystemService;

        private WrapperModelicaDomService mdlDomService;

        /// <summary>
        /// Initializes a new instance of the <see cref="CoreModule"/> class.
        /// </summary>        
        public CoreModule() {
            this.container = container = CoreServices.Container;
        }

        /// <summary>
        /// Initializes this instance and register global services for other modules.
        /// </summary>
        public void Initialize() {
            
            container.Resolve<IMightLogger>().Log( this , "Core module init" );
            
            authService = new AuthenticationService();            
            svnService = new SVNFileSystemService();
            localFileSystemService = new LocalFileSystemService();

            container.RegisterInstance<IProgressService>( new ProgressService() );

            dom = new DomRoot();
            container.RegisterInstance<IDom>(dom);

            // TODO find out, where to register common Icon annotation types like Icon, rectangle., etc.
            typeRegister = new AnotTypeRegister();
            container.RegisterInstance(typeRegister);
            //TODO Here?
            Modelica.Implementation.Initialize(typeRegister);

            AST.Ast.FillDom( dom );
           
            iconProvider = new IconProvider();
            container.RegisterInstance<IIconProvider>( iconProvider );

            mdlDomService = new WrapperModelicaDomService();
            container.RegisterInstance<IModelicaLanguageService>(mdlDomService);
        }
    }
}
